package dao;

import java.util.List;
import java.util.Map;

import bean.Role;
import utils.DaoUtil;

public class RoleDao {

	public List<Role> pageQueryData(Map<String, Object> map){
		String queryText = (String)map.get("queryText");
		String sql = null;
		if(queryText!=null){
			sql = "select * from roles where name like concat('%',?,'%') limit ?,?";
			return DaoUtil.queryBeanList(sql, Role.class, queryText,map.get("start"),map.get("size"));
		}
		else{
			sql = "select * from roles limit ?,?";
			return DaoUtil.queryBeanList(sql, Role.class, map.get("start"),map.get("size"));
		}
	}

	public int pageQueryCount(Map<String, Object> map){
		String queryText = (String)map.get("queryText");
		String sql = null;
		if(queryText!=null){
			sql = "select count(*) from roles where name like concat('%',?,'%')";
			return DaoUtil.count(sql, queryText);
		}
		else{
			sql = "select count(*) from roles";
			return DaoUtil.count(sql);			
		}
	}

	
	public List<Role> queryAll(){
		String sql = "select * from roles";
		return DaoUtil.queryBeanList(sql, Role.class);
	}

	public void insertRolePermission(Map<String, Object> paramMap){
		String roleid = (String)paramMap.get("roleid");
		String[] permissionids=(String[])paramMap.get("permissionids");
		if(permissionids!=null){
			for(String permissionid:permissionids){
				String sql = "insert into role_permission (roleid, permissionid) values(?,?)";
				DaoUtil.insert(sql, roleid,permissionid);
			}
		}
	}

	public void deleteRolePermissions(Map<String, Object> paramMap){
		String sql = "delete from role_permission where roleid =?";
		DaoUtil.delete(sql, paramMap.get("roleid"));
	}

}
